package cn.itsource.pet.service.impl;

import cn.itsource.basic.constant.PetHomeConstans;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.DistanceUtil;
import cn.itsource.basic.util.Point;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.SearchMapper;
import cn.itsource.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private SearchMapper searchMapper;
    @Autowired
    private ShopMapper shopMapper;

    /**
     * 添加寻主信息
     * @param searchMasterMsg
     */
    @Override
    @Transactional
    public void publish(SearchMasterMsg searchMasterMsg) {
        //根据收货地址将消息发布给最近的店铺
        //把地址转换为经纬度
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        //根据经纬度获取最近的店铺
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.selectAll());
        //将最近的店铺放入SearchMasterMsg
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setState(PetHomeConstans.SEARCH_MASTER);
        //添加
        searchMapper.save(searchMasterMsg);

    }
}
